package com.yungu.swift.system.sys.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.sys.dao.SysResourceMapper;
import com.yungu.swift.system.sys.model.dto.SysResourceDto;
import com.yungu.swift.system.sys.model.dto.SysUserDto;
import com.yungu.swift.system.sys.model.param.AdminResourceParam;
import com.yungu.swift.system.sys.service.SysResourceService;
import com.yungu.swift.system.sys.service.SysUserService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * SysResourceServiceImpl
 **/
@Service
public class SysResourceServiceImpl extends BaseServiceImpl<SysResourceDto> implements SysResourceService {
    @Autowired
    private SysResourceMapper sysResourceMapper;
    @Autowired
    private SysUserService sysUserService;

    @Override
    protected IMapper<SysResourceDto> getMapper() {
        return sysResourceMapper;
    }


    @Override
    public ResponseData<List<SysResourceDto>> queryAllResource(AdminParam adminParam) {
        String roleUuid = null;
        // 公司账户
        if (StringUtils.isNotEmpty(adminParam.getUserCompanyUuid())) {
            ResponseData<SysUserDto> getCompanyUser = sysUserService.get(MapUtils.build("companyUuid", adminParam.getUserCompanyUuid()));
            if (getCompanyUser.isSuccess() && getCompanyUser.getData() != null) {
                roleUuid = getCompanyUser.getData().getUserRole();
            }
        }
        // 代理账户
        if (roleUuid == null && StringUtils.isNotEmpty(adminParam.getUserAgentUuid())) {
            ResponseData<SysUserDto> getAgentUser = sysUserService.get(MapUtils.build("agentUuid", adminParam.getUserAgentUuid()));
            if (getAgentUser.isSuccess() && getAgentUser.getData() != null) {
                roleUuid = getAgentUser.getData().getUserRole();
            }
        }
        Map<String, Object> toMap = MapUtils.convertObjToMap(adminParam);
        toMap.put("roleUuid", roleUuid);
        return ResponseData.buildSuccessResponse(sysResourceMapper.queryAllResource(toMap));
    }

    @Override
    public ResponseData<List<SysResourceDto>> queryAuthHideBtn(Map<String, Object> map) {
        return ResponseData.buildSuccessResponse(sysResourceMapper.queryAuthHideBtn(map));
    }

    @Override
    public ResponseData<Boolean> saveOrUpdate(AdminResourceParam resourceParam) {
        SysResourceDto sysResourceDto = new SysResourceDto();
        BeanUtils.copyProperties(resourceParam, sysResourceDto);
        if (StringUtils.isEmpty(resourceParam.getUuid())) {
            sysResourceDto.setUuid(StringUtils.buildUUID());
            sysResourceDto.setResourceStatus(SysResourceDto.RES_STATUS_OK);
            sysResourceDto.setResourceParent(ParamUtil.defaultIfNull(resourceParam.getResourceParent(), "0"));
            sysResourceDto.setResourceSort(ParamUtil.defaultIfNull(resourceParam.getResourceSort(), 1));
            sysResourceDto.setCreateOn(new Date());
            sysResourceDto.setCreateBy(resourceParam.getUserUuid());
            return add(sysResourceDto);
        }
        sysResourceDto.setUpdateOn(new Date());
        sysResourceDto.setUpdateBy(resourceParam.getUserUuid());
        return edit(sysResourceDto);
    }
}